<!--#Include File = "CustomTemplate.asp" -->


<%
Dim txtUserName
Dim txtPassword
Dim cmdLogin

Dim mValidation
Call  PageController.RenderPage()

Public Function Page_Configure()
	PageController.SecuredHomeURL = "PublicPage.asp"
	PageController.PublicHomeURL  = "PublicPage.asp"
	PageController.PageTitle = "Login"
	PageController.RequiresAuthentication  = False
	PageController.PageCSSFiles = "main.css"
	PageController.BodyAttributes = " marginheight=0 marginwidth=0 topmargin=0 leftmargin=0 "
End Function 
%>

<%Public Function Page_RenderForm()%>	
	<BR><BR><BR><BR><BR>
	<%mValidation%>
	<TABLE align="center">
		<TR><td>User Name</td><td><%txtUserName%></td></TR>
		<TR><td>Password</td><td><%txtPassword%></td></TR>	
		<TR><td align=right colspan=2><Hr><%cmdLogin%></td></TR>		
	</TABLE>
	

<%End Function%>


<%
Dim sSQL
Dim rs
Dim roles
Dim userid
Dim username
Dim firstname
Dim lastname
Dim email

Public Function Page_Init()
	Set txtUserName = new_serverTextBox("txtUserName")
	Set txtPassword = new_serverTextBox("txtPassword")
	Set mValidation = New ServerValidationSummary
	Set cmdLogin = new_serverButton("cmdLogin")	

End Function

Public Function Page_Controls_Init()
	cmdLogin.Text = "Login"
	txtPassword.Mode = 2
	
End Function

':: POSTBACK HANDLERS
Public Function Validate()				
		mValidation.Validate 	txtUserName,"Username", VALIDATION_DATATYPE_STRING, True , ""		
		mValidation.Validate 	txtPassword,"Password", VALIDATION_DATATYPE_STRING, True , ""	
		
		'Quick and dirty... just for sample purposes :-?
	'I addes the setroles just for the sample because Auhenticate should do it.
	sSQL = "SELECT u.id as userid,* FROM [user] as u inner join permission as p on u.id = p.userid  inner join role as r on r.id = p.roleid Where u.status =1 and u.isdeleted =0 and u.username= '" & txtUserName.Text & "' and u.password = '" & txtPassword.Text & "'"
	'Response.Write(sSQL)
	Set rs = DBLayer.GetRecordSet(sSQL)
	ReDim roles(rs.RecordCount)		
	If rs.RecordCount = 0 Then
		mValidation.AddError("login false")
		Exit Function
	else
			
		Dim i
		i = 0
		While Not rs.EOF
			
				userid = rs("userid").Value
				username = rs("username").Value
				firstname = rs("firstname").Value
				lastname = rs("lastname").Value
				email = rs("email").Value
				roles(i) = rs("name").Value
				i = i+1			
				rs.MoveNext
			
		Wend
		'Response.Write("number role: " & UBound(roles))	
		'Exit Function
	End If
	End Function
Public Function cmdLogin_OnClick()
	
	
	Call Validate()
	If Not mValidation.IsValid Then
		Exit Function
	End If
		
	
	
	CurrentUser.RedirectFromLoginPage userid,username,firstname,lastname,email, roles
		
	
	
End Function

%>